package com.hansen.library.pickerimage.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.hansen.library.Constants;
import com.hansen.library.R;
import com.hansen.library.ui.activity.BaseTranBarActivity;
import com.hansen.library.ui.widget.viewpager.HackyViewPager;
import com.hansen.library.pickerimage.fragment.PicturePreviewFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 图片预览
 * Created by han on 2019/02/19
 */
public class PicturePreviewActivity extends BaseTranBarActivity implements ViewPager.OnPageChangeListener {

    //图片数量
    private TextView tv_preview_indicator;
    //保存
    private TextView tv_preview_save;
    //viewpage
    private HackyViewPager vp_pics_preview;

    private List<PicturePreviewFragment> mFragments;

    private List<String> imgUrls;

    private int mCurrentPosition = 0;

    @Override
    public int getTranBarResID() {
        return R.layout.activity_picture_preview;
    }

    @Override
    protected void initView() {
        tv_preview_indicator = findViewById(R.id.tv_preview_indicator);
        tv_preview_save = findViewById(R.id.tv_preview_save);

        vp_pics_preview = findViewById(R.id.vp_pics_preview);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mContext = this;

        if (getIntent() != null) {
            imgUrls = getIntent().getStringArrayListExtra(Constants.KeyUrl);
            mCurrentPosition = getIntent().getIntExtra(Constants.KeyPos, 0);
        }

        if (imgUrls == null || imgUrls.size() <= 0) {
            return;
        }

        mFragments = new ArrayList<>();

        PicturePreviewFragment previewFragment;

        for (int i = 0; i < imgUrls.size(); i++) {
            previewFragment = PicturePreviewFragment.newInstance(imgUrls.get(i));

            mFragments.add(previewFragment);
        }

        vp_pics_preview.setAdapter(new PictureViewPagerAdapter(getSupportFragmentManager()));

        vp_pics_preview.setCurrentItem(mCurrentPosition);// 设置当前所在的位置

        setIndicator(mCurrentPosition);//设置当前位置指示
    }

    @Override
    protected void initListener() {
        tv_preview_save.setOnClickListener(this);
        vp_pics_preview.addOnPageChangeListener(this);
    }

    @Override
    public void widgetClick(View v) {

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        mCurrentPosition = position;
        //页面变化时，设置当前的指示
        setIndicator(mCurrentPosition);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    private void setIndicator(int position) {
        if(imgUrls != null) {
            tv_preview_indicator.setText(position + 1 + "/" + imgUrls.size());//设置当前的指示
        }
    }


    class PictureViewPagerAdapter extends FragmentPagerAdapter {

        public PictureViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }
    }
}

